import {
	api
} from '@/untils/request.js'
export const wx_login = () => {
	let code = ''
	uni.login({
		provider: 'weixin',
		success: (loginRes) => {
			code = loginRes.code
		}
	})
	uni.getUserProfile({
		desc: '獲取您的昵稱、頭像、地區及性別',
		success: infoRes => {
			api('user/codev2', {
				code: code
			}).then(res => {
				api('user/loginv2', {
					encryptedData: infoRes.encryptedData,
					iv: infoRes.iv,
					sessionKey: res.data.session_key,
					openid: res.data.openid,
				}).then(v => {

					if (!v.data.mobile) {
						uni.showToast({
							title: '请绑定手机号',
							icon: "none",
						});
						setTimeout(() => {
							uni.navigateTo({
								url: `/pages/login/binding/binding?token=${v.data.token}`
							})
						}, 500)
						return
					}
					uni.setStorageSync('user', v.data)
					uni.setStorageSync('token', v.data.token)
					uni.showToast({
						title: '登陆成功',
						icon: "none",
					});
					setTimeout(() => {
						uni.switchTab({
							url: "/pages/index/index"
						})
					}, 500)

				})
			})
		}

	})

}
